package com.senbai.springlearning.threadlearn.controller;

import com.senbai.springlearning.threadlearn.thread.QueryInvoice;
import com.sun.xml.internal.ws.util.CompletedFuture;
import org.springframework.stereotype.Controller;

import java.util.concurrent.*;

@Controller
public class MutipleQueryLearn {

    LinkedBlockingQueue queue = new LinkedBlockingQueue();

    private static volatile ThreadPoolExecutor messageThreadPool = null;

    public static ThreadPoolExecutor getMessageThreadPool() {
        if(messageThreadPool == null){
            synchronized (ThreadPoolExecutor.class) {
                if(messageThreadPool == null){
                    /*
                     * 创建固定大小为1的线程池，使用ArrayBlockingQueue阻塞队列，队列大小为1万，线程数超过队列大小时的策略为重试。
                     */
                    messageThreadPool = new ThreadPoolExecutor(1,1,3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10000),
                            new ThreadPoolExecutor.CallerRunsPolicy());
                }
            }
        }
        return messageThreadPool;
    }

    public static void main(String[] args){

        CountDownLatch cdl = new CountDownLatch(1);
        for (int i = 0;i < 1000; i++){
            ThreadPoolExecutor executor = getMessageThreadPool();
            QueryInvoice queryInvoice = new QueryInvoice("103913",cdl);
            executor.submit(queryInvoice);

        }
        System.out.println("循环结束");
        cdl.countDown();

    }

}
